package com.zxbit.flash.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.common.RealNameAuthData;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FormAdapter extends RecyclerView.Adapter<FormAdapter.ContactViewHolder> {
    private Context context;
    private String[] key;
    private String[] value;

    public FormAdapter(Context context, String[] key, String[] value) {
        this.context = context;
        this.key = key;
        this.value = value;
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_form_detail, parent, false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ContactViewHolder holder, final int position) {
        holder.tvHint.setText(key[position]);

        if (holder.edContent.getTag() instanceof TextWatcher) {
            holder.edContent.removeTextChangedListener((TextWatcher) holder.edContent.getTag());
        }
        String[] arr = context.getResources().getStringArray(R.array.identity);
        if (holder.tvHint.getText().toString().equals(arr[1])||holder.tvHint.getText().toString().equals(arr[5])||
                holder.tvHint.getText().toString().equals(arr[12])||holder.tvHint.getText().toString().equals(arr[13])
                ||holder.tvHint.getText().toString().equals(arr[17])) {
            holder.edContent.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else {
            holder.edContent.setInputType(InputType.TYPE_CLASS_TEXT);
        }
        holder.edContent.setText(RealNameAuthData.getInstance().getData(key[position]));
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                String title = holder.tvHint.getText().toString();
                String message = holder.edContent.getText().toString();
                RealNameAuthData.getInstance().putData(title, message);
            }
        };

        holder.edContent.setTag(watcher);
        holder.edContent.addTextChangedListener(watcher);

    }

    @Override
    public int getItemCount() {
        return key.length;
    }

    public String[] getValue() {
        return value;
    }

    public class ContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_hint)
        TextView tvHint;
        @BindView(R.id.ed_content)
        EditText edContent;

        public ContactViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
